package com.lagou.dao.impl;

import com.lagou.dao.CourseContentDao;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 课程内容管理
 */
public class CourseContentDaoImpl implements CourseContentDao {
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT\n" +
                    "id,\n" +
                    "course_id,\n" +
                    "section_name,\n" +
                    "description,\n" +
                    "order_num,\n" +
                    "STATUS\n" +
                    "FROM course_section WHERE course_id = ?";
            //3.执行查询
            List<Course_Section> sectionList = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class),courseId);

            //根据章节ID查询课时信息
            for (Course_Section course_section : sectionList) {
                //调用方法  获取章节对应的课时
                List<Course_Lesson> lessonList = findLessonBySectionId(course_section.getId());
                course_section.setCourseLessonList(lessonList);
            }
            return sectionList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //根据章节id查询课时信息
    @Override
    public List<Course_Lesson> findLessonBySectionId(int courseId) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql ="SELECT\n" +
                    "id,\n" +
                    "course_id,\n" +
                    "section_id,\n" +
                    "theme,\n" +
                    "duration,\n" +
                    "is_free,\n" +
                    "order_num,\n" +
                    "STATUS\n" +
                    "FROM course_lesson WHERE section_id = ?";
            List<Course_Lesson> course_lessonList = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), courseId);
            return course_lessonList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据id回显课程信息
     * @param course
     * @return
     */
    @Override
    public Course findCourseByCourseId(int courseId) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT id,course_name FROM course WHERE id = ?";
            Course course = qr.query(sql, new BeanHandler<Course>(Course.class), courseId);
            return course;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存章节信息
     * @param course_section
     * @return
     */
    @Override
    public int saveSection(Course_Section course_section) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO course_section (course_id,section_name,description,order_num,STATUS,create_time,update_time)  VALUES (?,?,?,?,?,?,?)";
            Object[]param = {course_section.getCourse_id(),course_section.getSection_name(),course_section.getDescription(),course_section.getOrder_num(),
                    course_section.getStatus(),course_section.getCreate_time(),course_section.getUpdate_time()};
            int row = qr.update(sql, param);
            return row;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateSection(Course_Section course_section) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_section SET\n" +
                    "section_name = ?,\n" +
                    "description = ?,\n" +
                    "order_num = ?,\n" +
                    "update_time =? WHERE id = ?";
            Object[]param = {course_section.getSection_name(),course_section.getDescription(),course_section.getOrder_num(),course_section.getUpdate_time(),course_section.getId()};
            int row = qr.update(sql, param);
            return row;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateSectionStatus(int id, int status) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_section SET STATUS = ? ,update_time = ? WHERE id = ?";
            Object[]parapm = {status, DateUtils.getDateFormart(),id};
            int row = qr.update(sql, parapm);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 保存课时状态
     * @param course_lesson
     * @return
     */
    @Override
    public int saveLesson(Course_Lesson course_lesson) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO course_lesson (course_id,section_id,theme,duration,is_free,order_num,STATUS,create_time,update_time)  VALUES (?,?,?,?,?,?,?,?,?)";
            Object[]param = {course_lesson.getCourse_id(),course_lesson.getSection_id(),course_lesson.getTheme(),course_lesson.getDuration(),course_lesson.getIs_free(),course_lesson.getOrderNum(),
                            course_lesson.getStatus(),course_lesson.getCreate_time(),course_lesson.getUpdate_time()
            };
            int row = qr.update(sql, param);
            return row;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateLesson(Course_Lesson course_lesson) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_lesson SET\n" +
                    "section_id = ?,\n" +
                    "theme = ?,\n" +
                    "duration = ?,\n" +
                    "is_free = ?,\n" +
                    "order_num = ?,\n" +
                    "update_time =? WHERE id = ?";
            Object[]param = {course_lesson.getSection_id(),course_lesson.getTheme(),course_lesson.getDuration(),course_lesson.getIs_free(),course_lesson.getOrderNum(),course_lesson.getUpdate_time(),course_lesson.getId()};
            int row = qr.update(sql, param);
            return row;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
